import json
import time

import pytz
import requests
from email.utils import parsedate_to_datetime

from utils.tools import get_file_content


# 格式化时间 --self
# def format_last_modified(last_modified_str):
#     # 解析Last-Modified字符串为datetime对象
#     dt_gmt = parsedate_to_datetime(last_modified_str)
#
#     # 将GMT时间转换到中国标准时间
#     china_tz = pytz.timezone('Asia/Shanghai')
#     dt_cst = dt_gmt.astimezone(china_tz)
#
#     # 格式化datetime对象为特定格式字符串
#     formatted_date_str = dt_cst.strftime('%a %b %d %Y %H:%M:%S GMT+0800 (中国标准时间)')
#     return formatted_date_str
#
#
# # 获取线上图片地址二进制文件流数据以及图片尺寸和修改时间和图片名称 --self
# def get_file_content(image_url):
#     res = requests.get(image_url)
#     file_name = image_url.split('/')[-1]
#     last_modified_str = res.headers['last-modified']
#     type = res.headers['content-type']
#     if last_modified_str:
#         last_modified_str = format_last_modified(last_modified_str)
#     else:
#         last_modified_str = 'Fri Jan 26 2024 09:06:16 GMT+0800 (中国标准时间)'
#     size = res.headers['content-length']
#     if size:
#         size = str(size)
#     else:
#         size = '9999'
#     # 检查请求是否成功
#     if res.status_code == 200:
#         return {'content': res.content, 'size': size,
#                 'lastModifiedDate': last_modified_str, 'file_name': file_name, 'type': type}
#     else:
#         return None


# 上传图片返回结果
def upload_image(image_url, yh_ticket, photo_code, index):
    cookies = {
        'YH_TICKET': yh_ticket,
    }
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
    }
    params = {
        'key_Id': '',
        'photoCode': photo_code,
        'rules': '',
    }
    result = get_file_content(image_url)
    files = {
        'keyId': (None, ''),
        'id': (None, f'WU_FILE_{index}'),
        'name': (None, result['file_name']),
        'type': (None, result['type']),
        'lastModifiedDate': (None, result['lastModifiedDate']),
        'size': (None, result['size']),
        'file': (result['file_name'], result['content'], result['type']),
    }
    response = requests.post(
        'https://fx.cnpdx.com/pms/imgUpload/json/uploadImage',
        params=params,
        cookies=cookies,
        headers=headers,
        files=files,
    )
    response_json = json.loads(response.text)
    return response_json


# 根据出版社社名获取出版社社号
def get_pub_id_by_pub_name(pub_name, yh_ticket):
    cookies = {
        'YH_TICKET': yh_ticket,
    }
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
    }
    data = {
        'q': pub_name,
        'limit': '10',
        'timestamp': int(time.time() * 1000),
        'pressValue': pub_name,
        'flag': 'Y',
    }
    response = requests.post('https://fx.cnpdx.com/pms/system/json/findPublisher', cookies=cookies, headers=headers,
                             data=data)
    response_json = json.loads(response.text)
    if not response_json:
        return None
    else:
        return response_json[0]['pressNum']
